package com.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by XDLK on 2016/9/12.
 * <p>
 * Date： 2016/9/12
 */
public class DateUtils {

    public static final SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat formatYMDHMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date formatStr(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatDate(Object obj, String pattern) {
        Date dateTime = null;
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            if (obj instanceof String) {
                dateTime = format.parse((String) obj);
            } else if (obj instanceof Date) {
                dateTime = (Date) obj;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dateTime != null) {
            return format.format(dateTime);
        }
        return "";
    }

    /**
     * 根据日期，获取该日期的星期
     *
     * @param obj
     * @return
     */
    public static int getWeek(Object obj) {
        Date dateTime = null;
        try {
            if (obj instanceof String) {
                dateTime = formatYMD.parse((String) obj);
            } else if (obj instanceof Date) {
                dateTime = (Date) obj;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (dateTime != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateTime);
            if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
                return 7;
            } else {
                return calendar.get(Calendar.DAY_OF_WEEK) - 1;
            }
        }

        return 0;
    }

    /**
     * format 今天、昨天、前天、MM-dd HH:mm、yyyy-MM-dd HH:mm
     *
     * @param time
     * @return
     */
    public static String formatDate(Object time) {

        if (time == null) {
            return "未知时间";
        }
        Date thTime = getDate(time, formatYMDHMS);

        Calendar now = Calendar.getInstance();
        now.setTime(new Date());

        Calendar start = Calendar.getInstance();
        start.setTime(thTime);

        if (isYear(start, now)) { // 如果同一年

            if (isMonth(start, now)) { // 如果同一个月

                // 前天
                if (start.after(getTowYesterday()) && start.before(getYesterday())) {
                    return "前天 " + new SimpleDateFormat("HH:mm").format(thTime);
                } else if (start.after(getYesterday()) && start.before(getToDay())) {
                    return "昨天 " + new SimpleDateFormat("HH:mm").format(thTime);
                } else if (start.after(getToDay()) && start.before(getToMonDay())) {
                    return "今天 " + new SimpleDateFormat("HH:mm").format(thTime);
                } else {
                    return new SimpleDateFormat("MM-dd HH:mm").format(thTime);
                    //System.out.println(new SimpleDateFormat("MM-dd HH:mm").format(time));
                }
            } else {
                return new SimpleDateFormat("MM-dd HH:mm").format(thTime);
            }

        } else {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(thTime);
        }
    }

    public static String formatDate(Date startTime, Date endTime) {

        Calendar now = Calendar.getInstance();
        now.setTime(new Date());

        Calendar start = Calendar.getInstance();
        start.setTime(startTime);

        if (endTime != null) {

            // 交换两个时间的顺序
            Date tmpDate = startTime;
            if (!startTime.before(endTime)) {
                startTime = endTime;
                endTime = tmpDate;
            }

            String startStr = formatDate(startTime);
            String endStr = formatDate(endTime);

            String startPre = startStr.split(" ")[0];
            String endPre = endStr.split(" ")[0];
            if (startPre.equals(endPre)) {
                return startStr + (endStr.replace(endPre, "").replace(" ", "-"));
            } else {
                return startStr + "-" + endStr;
            }
        }

        return formatDate(startTime);
    }

    /**
     * 判断是否为同一年
     *
     * @param calendar
     * @return
     */
    private static boolean isYear(Calendar calendar, Calendar now) {

        if (now.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否为同一月
     *
     * @param calendar
     * @return
     */
    private static boolean isMonth(Calendar calendar, Calendar now) {

        if (now.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)) {
            return true;
        }
        return false;
    }

    public static boolean isDay(Calendar calendar, Calendar now){

        return true;
    }

    /**
     * 获取前两天时间
     *
     * @return
     */
    private static Calendar getTowYesterday() {
        Calendar yesterday = Calendar.getInstance();
        yesterday.set(Calendar.HOUR_OF_DAY, 23);
        yesterday.set(Calendar.MINUTE, 59);
        yesterday.set(Calendar.SECOND, 59);
        yesterday.set(Calendar.MILLISECOND, 1000);
        yesterday.add(Calendar.DATE, -3);
        return yesterday;
    }

    private static Calendar getThreeYesterday() {
        Calendar yesterday = Calendar.getInstance();
        yesterday.set(Calendar.HOUR_OF_DAY, 23);
        yesterday.set(Calendar.MINUTE, 59);
        yesterday.set(Calendar.SECOND, 59);
        yesterday.set(Calendar.MILLISECOND, 1000);
        yesterday.add(Calendar.DATE, -4);
        return yesterday;
    }

    /**
     * 获取昨天开始日期
     *
     * @return
     */
    private static Calendar getYesterday() {
        Calendar yesterday = Calendar.getInstance();
        yesterday.set(Calendar.HOUR_OF_DAY, 23);
        yesterday.set(Calendar.MINUTE, 59);
        yesterday.set(Calendar.SECOND, 59);
        yesterday.set(Calendar.MILLISECOND, 1000);
        yesterday.add(Calendar.DATE, -2);
        return yesterday;
    }

    /**
     * 获取今天开始时间
     *
     * @return
     */
    private static Calendar getToDay() {
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 23);
        today.set(Calendar.MINUTE, 59);
        today.set(Calendar.SECOND, 59);
        today.set(Calendar.MILLISECOND, 1000);
        today.add(Calendar.DATE, -1);
        return today;
    }

    private static Calendar getToMonDay() {
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 23);
        today.set(Calendar.MINUTE, 59);
        today.set(Calendar.SECOND, 59);
        today.set(Calendar.MILLISECOND, 1000);
        today.add(Calendar.DAY_OF_YEAR, 0);
        return today;
    }

    public static String toDayStr(Integer toDayCount) {
        return toDayStr(toDayCount, null);
    }

    public static String toDayStr(Integer toDayCount, Date dateTime) {
        Calendar cal = Calendar.getInstance();
        if (dateTime != null) {
            cal.setTime(dateTime);
        }

        cal.add(Calendar.DATE, toDayCount);
        return formatDate(cal.getTime(), "yyyy-MM-dd");
    }

    /**
     * 根据日期，获取本周开始、结束日期
     *
     * @param time
     * @return
     */
    public static Map<String, String> getCurWeek(Object time) {

        Map<String, String> map = new HashMap<>();

        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate(time, formatYMD));
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = formatYMD.format(cal.getTime());
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = formatYMD.format(cal.getTime());

        map.put("start", imptimeBegin);
        map.put("end", imptimeEnd);

        return map;
    }

    /**
     * 获取上周开始，结束时间
     *
     * @param time
     * @return
     */
    public static Map<String, String> getPreviouWeek(Object time) {

        Map<String, String> map = new HashMap<>();

        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate(time, formatYMD));
        cal.set(Calendar.DAY_OF_WEEK, 1);
        map.put("end", formatYMD.format(cal.getTime()));

        cal.add(Calendar.WEEK_OF_MONTH, -1);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        map.put("start", formatYMD.format(cal.getTime()));

        return map;
    }

    /**
     * 获取下一周开始，结束时间
     *
     * @param time
     * @return
     */
    public static Map<String, String> getNextWeek(Object time) {

        Map<String, String> map = new HashMap<>();

        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate("2016-09-20", formatYMD));

        cal.add(Calendar.WEEK_OF_MONTH, 1);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        map.put("start", formatYMD.format(cal.getTime()));

        cal.add(Calendar.DATE, 6);
        map.put("end", formatYMD.format(cal.getTime()));

        return map;
    }

    private static Date getDate(Object time, SimpleDateFormat format) {
        Date dateTime = null;
        try {
            if (time instanceof String) {
                dateTime = format.parse((String) time);
            } else if (time instanceof Date) {
                dateTime = (Date) time;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }
}
